import api from './api/index'
App({
  globalData: {
    name: 'min_qxb',
    qxhId: 79,
    deviceToken: ''
  },
  onLaunch() {
    // 登录
    if (!wx.getStorageSync('deviceToken')) {
      wx.login({
        success: res => {
          console.log(res.code)
          const qxhId = this.globalData.qxhId
          const name = `min_${qxhId}`
          api
            .loginGetDeviceToken({
              code: res.code,
              name
            })
            .then(result => {
              if (result.code && result.data.deviceToken) {
                this.globalData.deviceToken = result.data.deviceToken
                wx.setStorage({
                  key: 'deviceToken',
                  data: result.data.deviceToken,
                  success() {
                    wx.getStorage({
                      key: 'deviceToken',
                      success(res) {
                        if (res.data) {
                          api.HSQxhDeviceReport({
                            common: {
                              deviceToken: res.data
                            }
                          })
                        }
                      }
                    })
                  }
                })
              } else {
                wx.showToast({
                  title: result.message,
                  icon: 'error'
                })
              }
            })
        }
      })
    }
  }
})
